package com.huike.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebScoket配置处理器
 */
@Component("webSocketConfig")
//配置开启WebSocket服务用来接收ws请求
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

	//注入处理器
	@Autowired
	private ChatWebSocketHandler webSocketHandler;
	@Autowired
	private ChatHandshakeInterceptor chatHandshakeInterceptor;

	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		//为websocket请求的拦截与处理进行配置，/ws表示路径
		registry.addHandler(webSocketHandler, "/ws").setAllowedOrigins("*").addInterceptors(chatHandshakeInterceptor);
		//考虑到浏览器的websocket兼容性另加一个注册方式
		registry.addHandler(webSocketHandler, "/ws/sockjs").setAllowedOrigins("*").addInterceptors(chatHandshakeInterceptor).withSockJS();
	}
	

}
